﻿import com.esri.ags.layers.LayerInfo;
import com.esri.ags.events.LayerEvent;
import com.esri.ags.layers.ArcIMSMapServiceLayer;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import com.esri.ags.layers.Layer;
import flash.net.registerClassAlias;
import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.events. * ;
import com.uprr.app.tnm.events.AppEvent;


private var layerInfos : Array;
private var _layer : Layer;
private var _layerChanged : Boolean;


public function get layer () : Layer
{
	return _layer;
}

public function set layer (value : Layer) : void
{
	_layer = value;
	_layerChanged = true;
	invalidateProperties ();
}

override protected function commitProperties () : void
{
	if (_layerChanged)
	{
		_layerChanged = false;
		if (layer)
		{
			layer.addEventListener (Event.COMPLETE, removeBusyCursor, false, 0, true);
			layer.addEventListener (IOErrorEvent.IO_ERROR, removeBusyCursor, false, 0, true);
			if (layer.loaded)
			{
				setDataProvider ();
			} 
			else
			{
				layer.addEventListener (LayerEvent.LOAD, layerLoadHandler, false, 0, true);
			}
		}
	}
	// calling this last fixes an issue when the layer is set late via AS
	super.commitProperties ();
}
private function setDataProvider () : void
{
	var visibleLayers : ArrayCollection;
	if (layer is ArcGISDynamicMapServiceLayer)
	{
		layerInfos = ArcGISDynamicMapServiceLayer (layer).layerInfos;
		visibleLayers = ArcGISDynamicMapServiceLayer (layer).visibleLayers;
		
		/*
		* Trun off the layer group by default
		*/
		
		for (var i : int = 0; i < layerInfos.length; i ++)
		{
			if (layerInfos [i].subLayerIds != null)
			{
				layerInfos [i].defaultVisibility = false;
				var idIndex : int = visibleLayers.getItemIndex (layerInfos[i].id);
				if (idIndex != - 1)
				{
					visibleLayers.removeItemAt (idIndex);
				}
			}
		}
	} 
	 // not used
	else if (layer is ArcIMSMapServiceLayer)
	{
		layerInfos = ArcIMSMapServiceLayer (layer).layerInfos;
	}
	
	
	// make sure copy has typed LayerInfo objects
	registerClassAlias ("com.esri.ags.layers.LayerInfo", LayerInfo);
	// create a copy since it'll be modified to reflect active selections
	layerInfos = ObjectUtil.copy (layerInfos) as Array;
	dataProvider = layerInfos;
}
private function layerLoadHandler (event : LayerEvent) : void
{
	setDataProvider ();
}
public function showLayer (layerInfo : LayerInfo) : void
{
	var visibleLayers : ArrayCollection;
	if (layer is ArcGISDynamicMapServiceLayer)
	{
		visibleLayers = ArcGISDynamicMapServiceLayer (layer).visibleLayers;
		visibleLayers.addItem (layerInfo.id);
		// add id
	} 
	else if (layer is ArcIMSMapServiceLayer)
	{
		visibleLayers = ArcIMSMapServiceLayer (layer).visibleLayers;
		visibleLayers.addItem (layerInfo.name);
		// add name
	}
	if (visibleLayers)
	{
		cursorManager.setBusyCursor ();
	}
}
public function hideLayer (layerInfo : LayerInfo) : void
{
	var visibleLayers : ArrayCollection;
	if (layer is ArcGISDynamicMapServiceLayer)
	{
		visibleLayers = ArcGISDynamicMapServiceLayer (layer).visibleLayers;
		var idIndex : int = visibleLayers.getItemIndex (layerInfo.id);
		if (idIndex != - 1)
		{
			visibleLayers.removeItemAt (idIndex);
		}
	} 
	else if (layer is ArcIMSMapServiceLayer)
	{
		visibleLayers = ArcIMSMapServiceLayer (layer).visibleLayers;
		var nameIndex : int = visibleLayers.getItemIndex (layerInfo.name);
		if (nameIndex != - 1)
		{
			visibleLayers.removeItemAt (nameIndex);
		}
	}
	if (visibleLayers)
	{
		cursorManager.setBusyCursor ();
	}
}
private function removeBusyCursor (event : Event) : void
{
	cursorManager.removeBusyCursor ();
}


/**
 * Handle later icon updating when scrolling
 * 
 * @usage   
 * @return  
 */

/*
private function updateIcons(event:ScrollEvent):void{
	this.dataProvider.refresh();
	this.scrollToIndex(event.position);
	//Alert.show('icon is updated');

}
*/

// overlay map service click handler
private function itemDoubleClickHandler(event:ListEvent):void {
	//var str:String = new String(event.itemRenderer.data.id);
	var dcEvent : AppEvent = new AppEvent (AppEvent.TOC_DOUBLECLICK);
	dcEvent.data = event.itemRenderer.data.id;
	dcEvent.dispatch ();
}

private function dataGridInit(event:FlexEvent):void {
	event.currentTarget.itemRenderer.properties = {doubleClickEnabled:true};
}
